package study2018.com.xtianzhuang.www.luncene;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class LunceneReader {
	private static final String INDEX_PATH = "D:\\study\\luncene\\index";
	private static Logger logger = Logger.getLogger(LunceneReader.class);

	public static void main(String[] args) throws CorruptIndexException, IOException, ParseException {
		Directory directory = FSDirectory.open(new File(INDEX_PATH));
		IndexReader reader = IndexReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		QueryParser queryParser = new QueryParser(Version.LUCENE_36, "context", new StandardAnalyzer(Version.LUCENE_36));
		Query query = queryParser.parse("绝对秋香");
		/*
		 * public TopDocs search(Query query, int n) throws IOException { return search(query, null, n); }
		 * 
		 * public TopDocs search(Query query, Filter filter, int n) throws IOException { return search(createNormalizedWeight(query), filter, n); }
		 *
		 * 
		 * public TopDocs search(Weight weight, Filter filter, int nDocs) throws IOException { return search(weight, filter, null, nDocs); }
		 *
		 *
		 * protected TopDocs search(Weight weight, Filter filter, ScoreDoc after, int nDocs) throws IOException { if (this.executor == null) { int limit =
		 * this.reader.maxDoc(); if (limit == 0) { limit = 1; } nDocs = Math.min(nDocs, limit); TopScoreDocCollector collector =
		 * TopScoreDocCollector.create(nDocs, after, !weight.scoresDocsOutOfOrder()); search(weight, filter, collector); return collector.topDocs(); } HitQueue
		 * hq = new HitQueue(nDocs, false); Lock lock = new ReentrantLock(); ExecutionHelper<TopDocs> runner = new ExecutionHelper(this.executor); for (int i =
		 * 0; i < this.subReaders.length; i++) { runner.submit(new MultiSearcherCallableNoSort(lock, this.subSearchers[i], weight, filter, after, nDocs, hq)); }
		 * int totalHits = 0; float maxScore = Float.NEGATIVE_INFINITY; for (TopDocs topDocs : runner) { if (topDocs.totalHits != 0) { totalHits +=
		 * topDocs.totalHits; maxScore = Math.max(maxScore, topDocs.getMaxScore()); } } ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()]; for (int i = hq.size() -
		 * 1; i >= 0; i--) { scoreDocs[i] = ((ScoreDoc)hq.pop()); } return new TopDocs(totalHits, scoreDocs, maxScore); }
		 */

		/**
		 * public class TopDocs implements Serializable
		 * 
		 * {
		 * 
		 * public int totalHits;
		 * 
		 * public ScoreDoc[] scoreDocs;
		 * 
		 * private float maxScore;
		 */
		TopDocs topDocs = searcher.search(query, 10);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (ScoreDoc scoreDoc : scoreDocs) {
			/**
			 * public class ScoreDoc implements Serializable
			 * 
			 * { public float score;
			 * 
			 * public int doc;
			 * 
			 * public int shardIndex;
			 * 
			 */
			Document document = searcher.doc(scoreDoc.doc);
			logger.debug(document.get("context"));

		}

	}
}
